home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / LLIST.ZIP / BYTES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-26  |  7.0 KB  |  277 lines

  1. /*------------------------------------------------------------
  2. | FILE NAME: Bytes.c
  3. |
  4. | DOCUMENT: [1011.0]
  5. |
  6. | PURPOSE: To provide source code for byte-addressed memory 
  7. |          access procedures.
  8. |
  9. | DESCRIPTION: 
  10. |
  11. | NOTE: 
  12. |
  13. | HISTORY: 02.03.93 by Lee Malone from Bytes.txt.
  14. |          08.28.93 converted to new-style declarations
  15. ------------------------------------------------------------*/
  16.  
  17. #include <Bytes.h>
  18.  
  19. /* ---------------------- EQUATES ------------------------- */
  20.  
  21. /* ------------------------ DATA -------------------------- */
  22. Quad CountOfBytesParsed; /* Result count */
  23.  
  24. /* --------------------- PROCEDURES ----------------------- */
  25.  
  26. /*------------------------------------------------------------
  27. | NAME: CompareBytes
  28. |
  29. | PURPOSE: To compare two ranges of bytes based on their 
  30. |          unsigned numeric ordering.
  31. |
  32. | DESCRIPTION: Comparison operation.
  33. |              Returns: 0 if string AA = string BB.
  34. |                       positive number if AA > BB.
  35. |                       negative number if AA < BB.
  36. |
  37. | EXAMPLE:  Result = CompareBytes(A,ACount,B,BCount);
  38. |
  39. |
  40. | NOTE: You must cast the count parameters of 'CompareBytes'
  41. |       to be a 'Quad' to avoid errors when using 'Think C'.
  42. |
  43. |       If both byte ranges match over the length of the 
  44. |       shorter range, then the longer range follows the
  45. |       shorter in terms of order.
  46. |
  47. | ASSUMES: 
  48. |
  49. | HISTORY:  09.19.89 by Lee Malone 
  50. |           02.15.93 changed count to quad.
  51. |           11.10.93 simplified logic
  52. ------------------------------------------------------------*/
  53. Comparison
  54. CompareBytes(AddressOfByte A,
  55.              Quad          ACount, 
  56.              AddressOfByte B, 
  57.              Quad          BCount)
  58. {
  59.     Quad    MinCount;
  60.     Byte    AByte;
  61.     Byte    BByte;
  62.     
  63.     MinCount = ACount;
  64.     if( BCount < MinCount ) MinCount = BCount;
  65.  
  66.     /* compare for the length of the shortest range */
  67.     while( MinCount-- )   
  68.     {
  69.         AByte = *A++;
  70.         BByte = *B++;
  71.         
  72.         if(AByte != BByte)
  73.         {
  74.             return(((Comparison) AByte) - ((Comparison) BByte));
  75.         }   
  76.     }
  77.  
  78.     /* if unequal length ranges with first MinCount characters equal */
  79.     if( ACount != BCount ) 
  80.     {
  81.         return( (Comparison) ACount - (Comparison) BCount ); 
  82.     }
  83.     
  84.     return((Comparison) 0);    /* else ranges are equal */
  85. }
  86.  
  87. /*------------------------------------------------------------
  88. | NAME: CopyBytes
  89. |
  90. | PURPOSE: To copy a range of bytes from one place to another.
  91. |
  92. | DESCRIPTION: Correctly handles overlapping series of bytes. 
  93. |
  94. | EXAMPLE:  CopyBytes( From, To, HowMany );
  95. |
  96. | NOTE: You must cast literal count parameters to be a 'Quad' 
  97. |       to avoid errors when using 'Think C'. 
  98. |
  99. | ASSUMES: 
  100. |
  101. | HISTORY: 08.25.89 by Lee Malone
  102. |          08.31.89 added overlapping capability
  103. |          02.03.93 assembler version derived from Forthmacs 
  104. |                   via AM.
  105. |          09.22.93 replaced stack relative argument 
  106. |                   addresses with names
  107. |          11.01.93 assembler version replaced with general 
  108. |                   'C' version.
  109. |                   See 'FastBytes.c' for assembler version.
  110. ------------------------------------------------------------*/
  111. Nothing
  112. CopyBytes( AddressOfByte From, 
  113.            AddressOfByte To, 
  114.            Quad          Count )
  115. {
  116.     if( From >= To )
  117.     {
  118.         while( Count-- )
  119.         {
  120.             *To++ = *From++;
  121.         }
  122.     }
  123.     else
  124.     {
  125.         To   += Count;
  126.         From += Count;
  127.         
  128.         while( Count-- )
  129.         {
  130.             *--To = *--From;
  131.         }
  132.     }
  133.         
  134. }
  135.  
  136. /*------------------------------------------------------------
  137. | NAME: ExchangeBytes
  138. |
  139. | PURPOSE: To exchange the values in two non-overlapping 
  140. |          ranges of bytes.
  141. |
  142. | DESCRIPTION:  
  143. |
  144. | EXAMPLE:  ExchangeBytes( SourceA, SourceB, Count );
  145. |
  146. | NOTE:  
  147. |
  148. | ASSUMES: 
  149. |
  150. | HISTORY: 12.01.90 Create by Lee Malone from AM.
  151. |          09.19.91 revised for Focus.
  152. |          02.03.93 revised for WM.
  153. ------------------------------------------------------------*/
  154. Nothing
  155. ExchangeBytes( AddressOfByte SourceA, 
  156.                AddressOfByte SourceB, 
  157.                Quad          Count )
  158. {
  159.     Byte    AByte;
  160.     
  161.     while( Count-- )
  162.     {
  163.         AByte    = *SourceA;
  164.         *SourceA = *SourceB;
  165.         *SourceB = AByte;
  166.         SourceA++;
  167.         SourceB++;
  168.     }
  169. }
  170.  
  171. /*------------------------------------------------------------
  172. | NAME: FillBytes
  173. |
  174. | PURPOSE: To fill a range of bytes with a byte value.
  175. |
  176. | DESCRIPTION:  
  177. |
  178. | EXAMPLE:  FillBytes( Destination, ByteCount, ByteValue );
  179. |
  180. | NOTE: 'Pair' used as argument instead of 'Byte' because
  181. |       Think C can't pass 'Byte' arguments properly. See
  182. |       'DataSize.h' for more.
  183. |
  184. | ASSUMES: 
  185. |
  186. | HISTORY: 08.25.89 by Lee Malone
  187. |          11.10.93 changed argument types to 'Pair'.
  188. ------------------------------------------------------------*/
  189. Nothing
  190. FillBytes( AddressOfByte Destination, 
  191.            Quad            ByteCount, 
  192.            Pair            ByteValue )
  193. {
  194.     while( ByteCount-- )
  195.     {
  196.         *Destination++ = ByteValue;
  197.     }
  198. }
  199.  
  200. /*------------------------------------------------------------
  201. | NAME: IsMatchingBytes
  202. |
  203. | PURPOSE: To tell if two ranges of bytes match in value.
  204. |
  205. | DESCRIPTION: Returns 'True' if they match, else 'False'. 
  206. |
  207. | EXAMPLE:  
  208. |
  209. |    Result = IsMatchingBytes( SomeBytes, OtherBytes, 10 );
  210. |
  211. | NOTE:  
  212. |
  213. | ASSUMES: 
  214. |
  215. | HISTORY: 10.21.89 by Lee Malone
  216. |          02.15.93 changed count to quad.
  217. ------------------------------------------------------------*/
  218. Truth
  219. IsMatchingBytes( AddressOfByte AA, 
  220.                  AddressOfByte BB, 
  221.                  Quad          ACount )
  222. {
  223.     Truth        Result;
  224.  
  225.     Result = True;
  226.  
  227.     while( ACount-- )            
  228.     {
  229.         if(*AA != *BB )
  230.         {
  231.             Result = False;
  232.             break;
  233.         }
  234.         AA++;
  235.         BB++;
  236.     }
  237.     return( Result );         
  238. }
  239.   
  240. /*------------------------------------------------------------
  241. | NAME: ReplaceBytes
  242. |
  243. | PURPOSE: To replace all occurances of a byte in a range of 
  244. |          bytes.
  245. |
  246. | DESCRIPTION:  
  247. |
  248. | EXAMPLE:  
  249. |            ReplaceBytes( TokenBuffer, (Quad) 100, 
  250. |                          (Pair) 'a', (Pair) 'A' );
  251. |
  252. | NOTE: 'Pair' used as parameters instead of 'Byte' because
  253. |       Think C can't pass 'Byte' arguments properly. See
  254. |       'DataSize.h' for more.
  255. |
  256. | ASSUMES: 
  257. |
  258. | HISTORY: 04.04.91 by Lee Malone
  259. |          11.10.93 changed argument types to 'Pair'.
  260. ------------------------------------------------------------*/
  261. Nothing
  262. ReplaceBytes( AddressOfByte BaseAddress, 
  263.               Quad             Count, 
  264.               Pair            FindByte, 
  265.               Pair             ReplaceWithByte )
  266. {
  267.     while(Count--)
  268.     {
  269.         if( *BaseAddress == FindByte ) 
  270.         {
  271.             *BaseAddress = ReplaceWithByte;
  272.         }
  273.         BaseAddress++;
  274.     }
  275. }
  276.  
  277.